0

In my ui.R I am trying to capture the number entered from a textbox

textInput(inputId="dataSize", label="Choose Number of Rows", value = 1000)

And in my server.R I am trying to reactively get this value and create the output

datasetInputNumber <- reactive ({ input$dataSize })
output$number <- renderPrint({   datasetInputNumber()  })

I then have a separate .R file where I want to pass this number to a sql query

query <- sprintf("select * FROM Table limit %s;", limit)
result <- dbGetQuery(connectionString, query)

How can I get the output number into this previous .R file and set it to "limit"? The .R file is sitting in the correct directory and is being loaded by the server.R

Thanks,

0

1 Answer 1

1

You'll just need to run that command inside a reactive expression. So if you already have a function defined in your external .R file that wraps your query, you can call that like so:

output$query <- reactive({
  df <- runQuery(datasetInputNumber())

  # whatever you want to do to your data here.
  output$avg <- mean(df[,2])
  ...
})

where runQuery is the function defined in the external R file that accepts a number that then gets used in the SQL query.

Sign up to request clarification or add additional context in comments.

3 Comments

so how can I set the out$query to a dataframe?
because if the sql qery is inside the function then the resulting dataframe can only be used if it is returned by the function...but then this return value goes where? to the outpu$query? How can I convert this to an R object that is a data frame?
Write whatever code you want to process the dataframe within the reactive call. See the edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.