[英]how to integerate R script in Shiny web page?
In R script, I built a svm model by provide training. 在R脚本中,我通过提供培训构建了一个svm模型。 and in shiny webpage i designed user interface in which one textbox and two textarea are placed to get input from user.
在闪亮的网页中,我设计了一个用户界面,其中放置了一个文本框和两个文本区域以从用户那里获得输入。
Now i want that when user submit on button the textbox and textarea value should be used by R script program in which a training is given to model and now new input is used to classify data by built in model. 现在我希望当用户提交按钮时,文本框和textarea值应该由R脚本程序使用,其中对模型进行训练,现在使用新输入通过内置模型对数据进行分类。
so how to pass text field value of shiny webpage to R script? 那么如何将闪亮网页的文本字段值传递给R脚本?
ui <- fluidPage( fluidRow(id="form-row",column(6,offset =3 ,id="form-col",p(id="mandatory","'*' Fields are manadatory"),div(id = "form",
div(id="p-container",p(id="form-header","Your Question")),
div(id="form-field-container",
textInput("title", "*Title", width='100%',placeholder="Your Title is here..."),
# verbatimTextOutput("value"),
textOutput("title_val"),
#for text area width 100%
tags$style(HTML(".shiny-input-container:not(.shiny-input-container-inline){width: 100%;}")),
textAreaInput("description", "*Description", width='100%',rows = 3, resize = "both",placeholder="Description..."),
textOutput("desc_val"),
textAreaInput("code", "*Code", width='100%',rows = 3, resize = "both",placeholder="Write your code..."),
textOutput("code_val"),
useShinyalert(),
actionButton("submit", "Submit", class = "btn-primary")
))
)
)
)
server <- function(input, output, session){
observeEvent( input$submit,{
title_val <- as.character(input$title)
desc_val <- as.character(input$description)
code_val <- as.character(input$code)
ques<- paste(desc_val, code_val,sep=" \n")
shinyalert( title_val, ques, type = "success")
})
}
shinyApp(ui=ui, server=server)
You could do something like this: 你可以这样做:
eval(parse(text = input$code))
You could use eval
and parse
to evaluate a string. 您可以使用
eval
和parse
来计算字符串。
library(shiny)
library(tidyverse)
library(shinyalert)
ui <- fluidPage( fluidRow(id="form-row",column(6,offset =3 ,id="form-col",p(id="mandatory","'*' Fields are manadatory"),div(id = "form",
div(id="p-container",p(id="form-header","Your Question")),
div(id="form-field-container",
textInput("title", "*Title", width='100%',placeholder="Your Title is here..."),
# verbatimTextOutput("value"),
textOutput("title_val"),
#for text area width 100%
tags$style(HTML(".shiny-input-container:not(.shiny-input-container-inline){width: 100%;}")),
textAreaInput("description", "*Description", width='100%',rows = 3, resize = "both",placeholder="Description..."),
textOutput("desc_val"),
textAreaInput("code", "*Code", width='100%',rows = 3, resize = "both",placeholder="Write your code..."),
textOutput("code_val"),
useShinyalert(),
actionButton("submit", "Submit", class = "btn-primary")
))
)
)
)
server <- function(input, output, session){
model <- reactive({
data <- mtcars # use your own data, might it be in a fileinput or some other data
result <- eval(parse(text = input$code)) # Should include a hint to what way your dataframe is called and how it looks.
result
})
observeEvent( input$submit,{
title_val <- as.character(input$title)
desc_val <- as.character(input$description)
code_val <- as.character(input$code)
ques<- paste(desc_val, code_val,sep=" \n")
shinyalert( title_val, ques, type = "success")
})
}
shinyApp(ui=ui, server=server)
But to be honest, I do not like the idea to parse and evaluate text from a userinput. 但说实话,我不喜欢从用户输入中解析和评估文本的想法。 Far too many possibilites to run into trouble.
太多的可能性会遇到麻烦。
The main advantage that someone who is not familiar with R is gone when I have to copy and paste a script as input 当我必须复制并粘贴脚本作为输入时,不熟悉R的人的主要优点就消失了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.