BaseballR
BaseballR

Reputation: 147

Shiny - Taking Text input into auxiliary function

I am new to Shiny and trying to build a more accessible input and output for a function I built. I am giving this to people that don't run R so trying to build something that runs my functions in the background and then spits out the answer.

I am having some trouble getting everything in the way that I want it unfortunately and dealing with a bunch of errors. However, here is my more pointed question:

The actual function that I want to run takes a name (in quotations as "Last,First") and a number.

PredH("Last,First",650)

So I want a shiny application that takes a name input and a number input that then runs this program and then spits back out a data table with my answer. So a couple of questions.

How do I get it in the right form to input into my equation on the server side script, do I need to return it in the function so it can be accessed using a function$table type access? (Right now I am just printing using cat() function in the console for the function but know that may not be usable for this type of application.

I want to return a dataframe that can be gotten at PredH14$table. How do I go about building that shiny?

Here is my code so far:

UI:

library(shiny)


shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Miles Per Gallon"),

  # Sidebar with controls to select the variable to plot against mpg
  # and to specify whether outliers should be included
  sidebarPanel(
    textInput("playername", "Player Name (Last,First):", "Patch,Trevor"),
   radioButtons("type", "Type:",
                 list("Pitcher" = "P",
                      "Hitter" = "H"
                      )),

    numericInput("PAIP", "PA/IP:", 550),
    submitButton("Run Comparables")


  ),
    mainPanel(
    textOutput("name")
        )

Server:

library(shiny)

shinyServer(function(input, output) {

sliderValues <- reactive({


    data.frame(
      Name = c("name", "PA"),

        Value = c(as.character(playername),
                  PAIP),

        stringsAsFactors=FALSE)
  })

name=input[1,2] 
PAIP=input[2,2] 
testing <- function(name,PAIP){ 
a=paste(name,PAIP) 
return(a) }
output$name=renderText(testing$a)


})

Upvotes: 4

Views: 2202

Answers (1)

B.Mr.W.
B.Mr.W.

Reputation: 19628

I am not quite sure I understood your question 100% but I clearly see you are wondering how to pass the input of the UI into the server and maybe, the other way back.

In your server code, clearly you are not getting any input from the UI. Basically you have created three input variables in your ui.R:

1. input$playername
2. input$type 
3. input$PAIP

And one output:

1. output$name

Just let you know, the function sliderValues <- reactive(..) is called every time there is any input from the input... like people click the dropdown list or people modifies words in the text box. You can even get started without the submit button just to get started. But the existence of the submit button actually makes everything easier. Create a submit button for an input form. Forms that include a submit button do not automatically update their outputs when inputs change, rather they wait until the user explicitly clicks the submit button.

So you can put your code in a way similar like this:

# server.R
library(shiny)
shinyServer(function(input, output) {

  sliderValues <- reactive({
      result <- ... input$playername ... input$type ... input$PAIP
      return(result)
  })

  output$name <- renderPlot/renderText (... sliderValues...)
})

# ui.R
library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Miles Per Gallon"),

  sidebarPanel(
    textInput("playername" ... ),
    radioButtons("type" ... ),
    numericInput("PAIP" ... ), 
    submitButton("...")
  ),

  mainPanel(
    textOutput/plotOutput...("name")
  )
 ))

In the end, check out the shiny example that might be what you want.

library(shiny)
runExample('07_widgets')

Upvotes: 4

Related Questions