使用R从XML文件中提取数据,并将其以行和列的形式显示。

huangapple go评论97阅读模式
英文:

Using R to pull data from an XML file to display it in rows and columns

问题

以下是翻译好的部分:

"I'm pulling data from an XML file and displaying a variable name and its value in two columns, per row. So, each row has two columns: each column has a variable name and its value."

"我正在从一个XML文件中提取数据,并在每行中显示一个变量名称和它的值,所以每行有两列:每列都有一个变量名称和它的值。"

"I'm getting the variable names and the values from an XML file called leaf_default.xml."

"我正在从一个名为leaf_default.xml的XML文件中获取变量名称和值。"

"Some variables in the XML have grand-grandchild nodes. The image I provided shows some of the grandchild nodes, and they are d13c, solver_min, a, theta_j, etc."

"XML中的一些变量具有更深层次的子节点。我提供的图像显示了其中一些子节点,它们是d13c、solver_min、a、theta_j等等。"

"If you look at the XML file, you'll notice that variables such as reftemp, atref, Ha, Hd, Topt, deltaS, a_deltaS_t, b_deltaS_t, c_deltaS_t, and more have grand-grandchild nodes."

"如果您查看XML文件,您会注意到像reftemp、atref、Ha、Hd、Topt、deltaS、a_deltaS_t、b_deltaS_t、c_deltaS_t等变量具有更深层次的子节点。"

"I hope what I explained so far makes sense."

"我希望到目前为止我解释的内容是清晰的。"

"What I'd like to do is create one row and two columns, in a column to display the variables and values of the grand-grandchild nodes."

"我想要做的是创建一行和两列,在一列中显示grand-grandchild节点的变量和值。"

"Here's the xml file:"

"这是XML文件:"

"and here's my existing code (you'll notice that I'm dynamically reading the variables and values, please keep this in mind):"

"这是我的现有代码(您会注意到我动态地读取变量和值,请记住这一点):"

(请注意,我已经省略了代码的详细内容,只提供了相关描述的翻译。)

英文:

I'm pulling data from an XML file and displaying a variable name and its value in two columns, per row. So, each row has two columns: each column has a variable name and its value.
I'm getting the variable names and the values from an XML file called leaf_default.xml.
Some variables in the XML have grand-grandchild nodes. The image I provided shows some of the grandchild nodes, and they are d13c, solver_min, a, theta_j, etc.
example of grandchild nodes being displayed
If you look at the XML file, you'll notice that variables such as reftemp, atref, Ha, Hd, Topt, deltaS, a_deltaS_t, b_deltaS_t, c_deltaS_t, and more have grand-grandchild nodes.

I hope what I explained so far makes sense.
What I'd like to do is create one row and two columns, in a column to display the variables and values of the grand-grandchild nodes. example of what I'd like to achieve

Here's the xml file:
<default>
<fnames>
<leaf>
<sys>'f_sys_enzymek'</sys>
<rh_or_vpd>'f_vpd_from_rh'</rh_or_vpd>
<sat_vp>'f_sat_vp_allen1998'</sat_vp>
<solver>'f_solver_brent'</solver>
<residual_func>'f_residual_func_leaf_Ar'</residual_func>
<assimilation>'f_assimilation_c3'</assimilation>
<photorespiration>'f_photorespiration_f1980'</photorespiration>
<semiana>'f_semiana_quad'</semiana>
<Acg>'f_Acg_farquhar1980'</Acg>
<Ajg>'f_Ajg_generic'</Ajg>
<Apg>'f_Apg_vonc2000'</Apg>
<etrans>'f_etrans_harley1992'</etrans>
<gas_diff>'f_gas_diff_ficks_ci'</gas_diff>
<Alim>'f_Alim_farquhar1980'</Alim>
<vcmax>'f_vcmax_lin'</vcmax>
<jmax>'f_jmax_power'</jmax>
<tcor_jmax>'f_scalar_none'</tcor_jmax>
<tpu>'f_tpu_lin'</tpu>
<k_pepc>'f_k_pepc_constant'</k_pepc>
<rd>'f_rd_lin_vcmax'</rd>
<rl_rd>'f_scalar_none'</rl_rd>
<gstar>'f_gstar_f1980'</gstar>
<ri>'f_r_zero'</ri>
<rs>'f_rs_medlyn2011'</rs>
<rb>'f_rb_leafdim'</rb>
<cica_ratio>'f_cica_ratio_constant'</cica_ratio>
<d13c>'f_d13c_classical'</d13c>
<tcor_asc>
<vcmax>'f_tcor_asc_Arrhenius'</vcmax>
<jmax>'f_tcor_asc_Arrhenius'</jmax>
<tpu>'f_tcor_asc_Arrhenius'</tpu>
<k_pepc>'f_tcor_asc_Arrhenius'</k_pepc>
<rd>'f_tcor_asc_Arrhenius'</rd>
<gstar>'f_tcor_asc_quadratic_bf1985'</gstar>
<tau>'f_tcor_asc_Q10'</tau>
<Kc>'f_tcor_asc_Arrhenius'</Kc>
<Ko>'f_tcor_asc_Arrhenius'</Ko>
</tcor_asc>
<tcor_des>
<vcmax>'f_tcor_des_modArrhenius'</vcmax>
<jmax>'f_tcor_des_modArrhenius'</jmax>
<tpu>'f_tcor_des_modArrhenius'</tpu>
<k_pepc>'f_tcor_des_modArrhenius'</k_pepc>
<rd>'f_scalar_none'</rd>
</tcor_des>
<tcor_dep>
<tpu>'f_tcor_dep_independent'</tpu>
<rd>'f_tcor_dep_independent'</rd>
<tau>'f_tcor_dep_independent'</tau>
</tcor_dep>
<deltaS>
<rd>'f_deltaS'</rd>
<vcmax>'f_deltaS'</vcmax>
<jmax>'f_deltaS'</jmax>
<tpu>'f_deltaS'</tpu>
<k_pepc>'f_deltaS'</k_pepc>
</deltaS>
<q10>
<rd>'f_q10_constant'</rd>
<vcmax>'f_q10_constant'</vcmax>
<jmax>'f_q10_constant'</jmax>
<k_pepc>'f_q10_constant'</k_pepc>
<tau>'f_q10_constant'</tau>
<Kc>'f_q10_constant'</Kc>
<Ko>'f_q10_constant'</Ko>
</q10>
</leaf>
</fnames>
<pars>
<leaf>
<d13c>FALSE</d13c>
<deltaA_prop>0.15</deltaA_prop>
<solver_min>-0.0029834</solver_min>
<solver_max>51.8364435</solver_max>
<a>0.8</a>
<f>0.23</f>
<quantum_yield_to_eff>4</quantum_yield_to_eff>
<ko_kc_ratio>0.21</ko_kc_ratio>
<theta_j>0.9</theta_j>
<theta_col_cj>0.95</theta_col_cj>
<theta_col_cjp>0.98</theta_col_cjp>
<avn_25>10</avn_25>
<bvn_25>30</bvn_25>
<ajv_25>29</ajv_25>
<bjv_25>1.63</bjv_25>
<a_jvt_25>2.59</a_jvt_25>
<b_jvt_25>-0.035</b_jvt_25>
<c_jvt_25>-0.0202</c_jvt_25>
<e_ajv_25>1.01</e_ajv_25>
<e_bjv_25>0.89</e_bjv_25>
<atv_25>0</atv_25>
<btv_25>0.166666666666667</btv_25>
<akv_25>0</akv_25>
<bkv_25>20000</bkv_25>
<flnr>0.09</flnr>
<fnr>7.16</fnr>
<Rsa>60</Rsa>
<Apg_alpha>0</Apg_alpha>
<g0>0.01</g0>
<g1_medlyn>4.16</g1_medlyn>
<g1_leuning>10</g1_leuning>
<d0>1</d0>
<g1_ball>6</g1_ball>
<g_a1_yin>0.85</g_a1_yin>
<g_b1_yin>0.14</g_b1_yin>
<rs>6.66666666666667</rs>
<cica_chi>0.7</cica_chi>
<rb>0.1</rb>
<can_ttc>0.01</can_ttc>
<leaf_width>0.1</leaf_width>
<ri>6.66666666666667</ri>
<co2_diff>1.7e-09</co2_diff>
<hco_co2_ratio>0</hco_co2_ratio>
<hco_co2_diff_ratio>0.56</hco_co2_diff_ratio>
<d13c_a>4.4</d13c_a>
<d13c_b>27</d13c_b>
<d13c_b_prime>30</d13c_b_prime>
<d13c_am>1.8</d13c_am>
<d13c_f>12</d13c_f>
<a_rdv_25>0</a_rdv_25>
<b_rdv_25>0.015</b_rdv_25>
<a_rdn_25>0.5</a_rdn_25>
<b_rdn_25>0.15</b_rdn_25>
<rl_rd_ratio>1</rl_rd_ratio>
<rl_rd_lloyd_a>0.5</rl_rd_lloyd_a>
<rl_rd_lloyd_b>0.05</rl_rd_lloyd_b>
<a_rdv_25_t>0.015</a_rdv_25_t>
<b_rdv_25_t>-5e-04</b_rdv_25_t>
<home_temp>25</home_temp>
<reftemp>
<rd>25</rd>
<vcmax>25</vcmax>
<jmax>25</jmax>
<tpu>25</tpu>
<k_pepc>25</k_pepc>
<Kc>25</Kc>
<Ko>25</Ko>
<gstar>25</gstar>
<tau>25</tau>
</reftemp>
<atref>
<rd>2</rd>
<vcmax>50</vcmax>
<jmax>100</jmax>
<tpu>5</tpu>
<k_pepc>7e+05</k_pepc>
<Kc>40.49</Kc>
<Ko>27.84</Ko>
<gstar>4.325</gstar>
<tau>2.6</tau>
<vomax>0</vomax>
</atref>
<Ha>
<rd>69830</rd>
<vcmax>69830</vcmax>
<jmax>100280</jmax>
<tpu>69830</tpu>
<k_pepc>69830</k_pepc>
<Kc>79430</Kc>
<Ko>36380</Ko>
<gstar>37830</gstar>
<tau>-41572</tau>
<vomax>60110</vomax>
</Ha>
<Hd>
<rd>2e+05</rd>
<vcmax>2e+05</vcmax>
<jmax>2e+05</jmax>
<tpu>2e+05</tpu>
<k_pepc>2e+05</k_pepc>
</Hd>
<Topt>
<rd>27.56</rd>
<vcmax>27.56</vcmax>
<jmax>19.89</jmax>
<tpu>27.56</tpu>
<k_pepc>27.56</k_pepc>
</Topt>
<deltaS>
<rd>0</rd>
<vcmax>0</vcmax>
<jmax>0</jmax>
<tpu>0</tpu>
<k_pepc>0</k_pepc>
</deltaS>
<a_deltaS_t>
<rd>490</rd>
<vcmax>668</vcmax>
<jmax>660</jmax>
<tpu>485</tpu>
</a_deltaS_t>
<b_deltaS_t>
<rd>0</rd>
<vcmax>-1.07</vcmax>
<jmax>-0.75</jmax>
<tpu>0</tpu>
</b_deltaS_t>
<c_deltaS_t>
<rd>0</rd>
<vcmax>0</vcmax>
<jmax>-0.52</jmax>
<tpu>0</tpu>
</c_deltaS_t>
<q10>
<rd>2</rd>
<vcmax>2</vcmax>
<jmax>2</jmax>
<tpu>2</tpu>
<k_pepc>2</k_pepc>
<Kc>2</Kc>
<Ko>2</Ko>
<tau>0.57</tau>
</q10>
<a_q10_t>
<rd>3.22</rd>
</a_q10_t>
<b_q10_t>
<rd>-0.046</rd>
</b_q10_t>
<tupp_cox>
<vcmax>36</vcmax>
<rd>45</rd>
</tupp_cox>
<tlow_cox>
<vcmax>0</vcmax>
<rd>5</rd>
</tlow_cox>
<exp_cox>
<vcmax>0.3</vcmax>
<rd>0.4</rd>
</exp_cox>
<gstar_bf_a>0.012</gstar_bf_a>
<gstar_bf_b>1.68</gstar_bf_b>
<gstar_bf_c>42.7</gstar_bf_c>
<R>8.31446</R>
</leaf>
</pars>
<env>
<leaf>
<ca_conc>400</ca_conc>
<o2_conc>0.21</o2_conc>
<par>1000</par>
<water_l>0</water_l>
<sphag_l>0</sphag_l>
<temp>25</temp>
<vpd>0</vpd>
<rh>0.8</rh>
<atm_press>101325</atm_press>
<wind>1</wind>
</leaf>
</env>
</default>

and here's my exiting code (you'll notice that I'm dynamically reading the variables and values, please keep this in mind):

library(shiny)
library(xml2)
library(shinyWidgets)
library(magrittr)
# Read the XML file and extract the options
xml_file <- "your_path_for_where_you_saved_the_xml_file/leaf_default.xml"
xml_data <- read_xml(xml_file)
# Find the "pars" parent node
pars_leaf_node <- xml_data %>% xml_find_first(".//pars/leaf")
# Find the grandchild node names under "pars"
pars_grandchild_node_names <- xml_children(pars_leaf_node) %>% xml_name()
pars_grandchild_node_values <- xml_children(pars_leaf_node) %>% xml_text()
pars_tc <- xml_data %>% xml_find_first(".//pars/leaf/tlow_cox")
pars_tc_names <- xml_children(pars_tc) %>% xml_name()
pars_tc_values <- xml_children(pars_tc) %>% xml_text()
# Define the UI
ui <- fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
prettyRadioButtons(
"parts",
"Please Select",
choices = "pars",
inline = TRUE,
status = "danger",
fill = TRUE 
),
width = '10%',
hr(),
uiOutput("ui")
),
mainPanel(
# Add the main panel content here
)
)
)
# Define the server
server <- function(input, output, session) {
# Update the UI based on the selected parent node
observeEvent(input$parts, {
grandchild_nodes <- pars_grandchild_node_names
output$ui <- renderUI({
fluidRow(
lapply(grandchild_nodes, function(node_name) {
if (node_name %in% pars_grandchild_node_names) {
# Replace the condition with the grandchild nodes you want as text inputs or dropdowns
node_index <- match(node_name, pars_grandchild_node_names)
default_value <- pars_grandchild_node_values[node_index]
if (node_name == "deltaA_prop") {
column(6, 
sliderInput(node_name, label = node_name, value = default_value, min = 0.0, max = 1.0)
)
} else if (node_name == "solver_max") {
column (6, 
numericInput(node_name, label = node_name, value = default_value, min = 0.0, max = 100.0, step = 0.01)
)
} else {
column(6,
textInput(node_name, label = node_name, value = default_value)
)
}
} 
# else {
#   # For other grandchild nodes, display as labels
#   column(6,
#          tags$label(node_name)
#   )
# }
}) 
)
})
})
# Update the input values when the user makes changes
observeEvent(input$ui, {
for (node_name in grandchild_nodes) {
if (node_name %in% pars_grandchild_node_names) {
updateTextInput(session, node_name, value = input[[node_name]])
}
}
})
}
shinyApp(ui = ui, server = server)

答案1

得分: 1

也许这会有所帮助。xml2::as_list()将所有节点转换为列表,然后可以从中生成数据框。

library(xml2)

page <- read_xml("<default>
  <fnames>
    <leaf>
      <sys>'f_sys_enzymek'</sys>
      <rh_or_vpd>'f_vpd_from_rh'</rh_or_vpd>
      <sat_vp>'f_sat_vp_allen1998'</sat_vp>
     .
     .
     .
  </default>")
  
# 将所有节点作为列表获取
nodelist <- as_list(page)

# 将列表转换为数据框
df <- data.frame(values = unlist(nodelist))

# 将行名称转换为列
df$node <- row.names(df)
row.names(df) <- NULL

df

结果:

                  values                                node
1          'f_sys_enzymek'         default.fnames.leaf.sys
2          'f_vpd_from_rh'   default.fnames.leaf.rh_or_vpd
3     'f_sat_vp_allen1998'      default.fnames.leaf.sat_vp
4         'f_solver_brent'      default.fnames.leaf.solver
5 'f_residual_func_leaf_Ar' default.fnames.leaf.residual_func
6       'f_assimilation_c3'   default.fnames.leaf.assimilation
英文:

Maybe this will help. The xml2::as_list() will convert all of the nodes into a list from there one can generate a data frame.

library(xml2)
page &lt;-read_xml(&quot;&lt;default&gt;
&lt;fnames&gt;
&lt;leaf&gt;
&lt;sys&gt;&#39;f_sys_enzymek&#39;&lt;/sys&gt;
&lt;rh_or_vpd&gt;&#39;f_vpd_from_rh&#39;&lt;/rh_or_vpd&gt;
&lt;sat_vp&gt;&#39;f_sat_vp_allen1998&#39;&lt;/sat_vp&gt;
.
.
.
&lt;default&gt;&quot;)
#get all of the nodes as a list
nodelist &lt;- as_list(page)
#convert the list to data frame
df &lt;- data.frame(values=unlist(nodelist))
#Convert rownames to a column
df$node &lt;- row.names(df)
row.names(df) &lt;- NULL
df
values                                   node
1                 &#39;f_sys_enzymek&#39;                default.fnames.leaf.sys
2                 &#39;f_vpd_from_rh&#39;          default.fnames.leaf.rh_or_vpd
3            &#39;f_sat_vp_allen1998&#39;             default.fnames.leaf.sat_vp
4                &#39;f_solver_brent&#39;             default.fnames.leaf.solver
5       &#39;f_residual_func_leaf_Ar&#39;      default.fnames.leaf.residual_func
6             &#39;f_assimilation_c3&#39;       default.fnames.leaf.assimilation

huangapple
  • 本文由 发表于 2023年6月8日 05:07:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定