2013-11-15 38 views
3

我是Shiny的新手,并试图为我构建的功能构建更易于访问的输入和输出。我把这个交给那些不运行R的人,所以试图在后台创建一些运行我的函数的东西,然后吐出答案。闪亮 - 将文本输入转换为辅助功能

我遇到了一些麻烦,不幸的是我处理了一堆错误。然而,这里是我更尖锐的问题:

我想要运行的实际功能需要一个名称(引用为“Last,First”)和一个数字。

PredH("Last,First",650) 

所以我想一个闪亮的应用程序,它需要一个名称输入,输入的号码是然后运行该程序,然后吐出背出一个数据表,我的答案。所以有几个问题。

如何获得它在正确的形式输入到我的公式在服务器端脚本,我需要返回它的功能,因此它可以访问使用函数$表类型访问? (现在我只是在控制台中使用cat()函数打印函数,但知道可能不适用于此类应用程序。

我想返回可在PredH14 $表中获得的数据帧。如何着手建立闪亮

这是到目前为止我的代码:?

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") 
     ) 

服务器:

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) 


}) 

回答

3

我不是很确定我理解你的问题100%,但我清楚地看到你想知道如何将UI的输入传递到服务器,也许,另一种方式。

在您的服务器代码中,显然您没有从UI获取任何输入。基本上你已经在你的ui.R创建了三个输入变量:

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

和一个输出:

1. output$name 

只是让你知道,功能sliderValues <- reactive(..)被称为每次有来自输入任何输入.. 。像人们点击下拉列表或人们修改文本框中的单词。 你甚至可以在没有submit button的情况下开始上手。但是提交按钮的存在实际上使得一切都变得简单。 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.

所以,你可以把你的代码,类似这样的方式:

# 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") 
) 
)) 

在最后,检查出有光泽的例子,可能是你想要的。

library(shiny) 
runExample('07_widgets') 
+0

感谢您的回答,它一直非常有帮助。尽管问题很快。我试图将这些输入传递到另一个将存在于服务器端的函数。假设我的功能是这样的: – BaseballR

+0

' name = input [1,2] PAIP = input [2,2] 测试< - function(name,PAIP){0} = paste(name,PAIP) 回报(一) }' ,然后要输出回到UI等等,然后做一些事情,如: '输出$名称= renderText(测试$一)' 是,你将如何得到的东西你功能? 谢谢!对不起,这个令人困惑的问题! – BaseballR

+0

我做这种类型的事情,总是回来的类型闭合对象不子集,我无法找到任何具体到闪亮的问题的答案。 – BaseballR